/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import Beans.Advertise;
import Beans.Company;
import Beans.DriverBean;
import Beans.Order;
import Beans.UserBean;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import radio.entities.Accounts;
import radio.entities.Driver;
import radio.entities.Feedback;
import radio.entities.Order1;
import radio.entities.PaymentDetails;
import radio.entities.PaymentDetailsPK;
import radio.sessions.AccountsFacade;
import radio.sessions.AdvertiseFacade;
import radio.sessions.CompanyFacade;
import radio.sessions.DriverFacade;
import radio.sessions.FeedbackFacade;
import radio.sessions.Order1Facade;
import radio.sessions.PaymentDetailsFacade;

/**
 *
 * @author Huu Vinh
 */
public final class Admin {
    @EJB
    private Order1Facade order1Facade;

    @EJB
    private FeedbackFacade feedbackFacade;
    @EJB
    private DriverFacade driverFacade;
    @EJB
    private AdvertiseFacade advertiseFacade;
    @EJB
    private CompanyFacade companyFacade;
    @EJB
    private PaymentDetailsFacade paymentDetailsFacade;
    @EJB
    private AccountsFacade accountsFacade;
    private UserBean user;
    private UserBean user1;
    private PaymentDetails pd;
    private PaymentDetailsPK pk;
    private String rePass;
    private String oldPass;
    private String newPass;
    private String joinDate;
    private String cityName;
    private String typeName;
    private String date;
    private String amount;

    public Admin() {
    }

    public PaymentDetailsPK getPk() {
        return pk;
    }

    public void setPk(PaymentDetailsPK pk) {
        this.pk = pk;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getJoinDate() {
        return joinDate;
    }

    public void setJoinDate(String joinDate) {
        this.joinDate = joinDate;
    }

    public UserBean getUser1() {
        return user1;
    }

    public void setUser1(UserBean user1) {
        this.user1 = user1;
    }

    public UserBean getUser() {
        return user;
    }

    public void setUser(UserBean user) {
        this.user = user;
    }

    public PaymentDetails getPd() {
        return pd;
    }

    public void setPd(PaymentDetails pd) {
        this.pd = pd;
    }

    public void setRePass(String rePass) {
        this.rePass = rePass;
    }

    public void setOldPass(String oldPass) {
        this.oldPass = oldPass;
    }

    public void setNewPass(String newPass) {
        this.newPass = newPass;
    }

    public String getNewPass() {
        return newPass;
    }

    public String getOldPass() {
        return oldPass;
    }

    public String getRePass() {
        return rePass;
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public void getAccount() {
//        HttpSession ses = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
//        temp = accountsFacade.find(Integer.parseInt((String)ses.getAttribute("adminId")));
        Accounts temp = new Accounts();
        temp = accountsFacade.find(0);
        if (temp != null) {
            user = new UserBean(temp.getUid(), temp.getPwd(), temp.getImg(), temp.getAccountDetails().getContactPerson(), temp.getAccountDetails().getMobile(), temp.getAccountDetails().getTelephone(), temp.getAccountDetails().getAddress(), temp.getAccountDetails().getEmail(), temp.isGender(), temp.getStatus(), temp.getAccId(), temp.getTypeId().getTypeId(), temp.getAccountDetails().getCityId().getCityId());
            joinDate = temp.getJoinDate().toString();
        } else {
            System.out.println("dkm null");
        }
    }

    public String show() {
        getAccount();
        return "infor.show();";
    }

    public List<PaymentDetails> showInfor() {
        return paymentDetailsFacade.getIdByStatus(false);
    }

    public String confirmPayment() {
        if (accountsFacade.confirmPayment(pd.getAccounts().getUid(), pd.getPaymentRate().getPaymentName(), pk)) {
            return "";
        }
        return null;
    }

    public List<Company> showCom() {
        List<radio.entities.Company> comtemp = companyFacade.findAll();
        List<Company> result = new ArrayList<Company>();
        for (radio.entities.Company company : comtemp) {
            Accounts acctemp = accountsFacade.find(company.getCompanyId());
            Company temp = new Company();
            temp.setUid(acctemp.getUid());
            temp.setContact(acctemp.getAccountDetails().getContactPerson());
            temp.setCompanyname(company.getCompanyName());
            temp.setDesignation(company.getDesignation());
            temp.setFax(company.getFax());
            temp.setMobile(acctemp.getAccountDetails().getMobile());
            temp.setTele(acctemp.getAccountDetails().getTelephone());
            result.add(temp);
        }
        return result;
    }

    public List<Advertise> showAd() {
        List<radio.entities.Advertise> adtemp = advertiseFacade.findAll();
        List<Advertise> result = new ArrayList<Advertise>();
        for (radio.entities.Advertise advertise : adtemp) {
            Advertise temp = new Advertise();
            temp.setUid(advertise.getAccounts().getUid());
            temp.setContact(advertise.getAccounts().getAccountDetails().getContactPerson());
            temp.setCompanyname(advertise.getCompanyName());
            temp.setDescription(advertise.getDescription());
            temp.setDesignation(advertise.getDesignation());
            temp.setFax(advertise.getFax());
            result.add(temp);
        }
        return result;
    }

    public List<DriverBean> showDriver() {
        List<DriverBean> result = new ArrayList<DriverBean>();
        List<Driver> drtemp = driverFacade.findAll();
        for (Driver driver : drtemp) {
            DriverBean temp = new DriverBean();
            temp.setUid(driver.getAccounts().getUid());
            temp.setContact(driver.getAccounts().getAccountDetails().getContactPerson());
            temp.setAddress(driver.getAccounts().getAccountDetails().getAddress());
            temp.setDescription(driver.getDesciption());
            temp.setExperience(driver.getExperience());
            temp.setMobile(driver.getAccounts().getAccountDetails().getMobile());
            result.add(temp);
        }
        return result;
    }

    public List<Beans.Feedback> showFeed() {
        List<Feedback> ftemp = feedbackFacade.findAll();
        List<Beans.Feedback> result = new ArrayList<Beans.Feedback>();
        for (Feedback feedback : ftemp) {
            Beans.Feedback temp=new Beans.Feedback();
            temp.setUid(feedback.getAccountId().getUid());
            temp.setType(feedback.getFBTypeId().getFBName());
            temp.setContent(feedback.getFBContent());
            result.add(temp);
        }
        return result;
    }
    
//    public List<Order> showOr(){
//        List<Order> result=new ArrayList<Order>();
//        List<Order1> otem=order1Facade.findAll();
//        for (Order1 order1 : otem) {
//            Order temp=new Order();
//            temp.setUid(order1.getAccId().getUid());
//            temp.setStr(order1.getOrderStart().toString());
//            temp.setEnd(order1.getOrderEnd().toString());
//            temp.setCity(order1.getCityId().getCityName());
//            temp.setContent(order1.getContent());
//            result.add(temp);
//        }
//        return result;
//    }

    public void onRowSelect(SelectEvent event) {
        pd = (PaymentDetails) event.getObject();
        System.out.println(pd.getAccounts());
        System.out.println(pd.getAmount());
        pk = ((PaymentDetails) event.getObject()).getPaymentDetailsPK();
    }

    public void changePassword() {
        FacesMessage msg = null;
        getAccount();
        boolean changed = false;
        if (!user.getPwd().equals(oldPass)) {
            msg = new FacesMessage("Old Password is wrong");
            changed = false;
        } else if (!newPass.matches("^[A-Za-z0-9_-]{4,15}$")) {
            msg = new FacesMessage("New Password is invalid");
            changed = false;
        } else if (!rePass.matches("^[A-Za-z0-9_-]{4,15}$")) {
            msg = new FacesMessage("New Password is invalid");
            changed = false;
        } else if (!newPass.equals(rePass)) {
            msg = new FacesMessage("Confirm Password is invalid");
            changed = false;
        } else {
            try {
                Accounts temp = accountsFacade.findbyId(0);
                temp.setPwd(newPass);
                accountsFacade.edit(temp);
                msg = new FacesMessage("Change Password is successfull!!!");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                changed = true;
            } catch (Exception ex) {
                msg = new FacesMessage("Error");
                FacesContext.getCurrentInstance().addMessage(null, msg);
                ex.printStackTrace();
                changed = false;
            }
        }
        FacesContext.getCurrentInstance().addMessage(null, msg);
        RequestContext.getCurrentInstance().addCallbackParam("changed", changed);
    }
}
